<?php
class Mysql{

	var $prefix;
	var $link_id;
	var $query_result;

	var $saved_queries = array();
	var $num_queries = 0;

	function Mysql($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect)
	{
		$this->prefix = $db_prefix;

		if ($p_connect)
			$this->link_id = @mysql_pconnect($db_host, $db_username, $db_password);
		else
			$this->link_id = @mysql_connect($db_host, $db_username, $db_password);

		if ($this->link_id)
		{
			if (@mysql_select_db($db_name, $this->link_id))
				return $this->link_id;
			else
				die('Unable to select database. MySQL reported: '.mysql_error().' '.__FILE__.':'.__LINE__);
		}
		else
			die('Unable to connect to MySQL server. MySQL reported: '.mysql_error().' '.__FILE__.':'.__LINE__);
	}
	
	function start_transaction()
	{
		return;
	}


	function end_transaction()
	{
		return;
	}

    // my_error returns (or send to standard output) the message concerning the
	// error occured for the last mysql query.
	
	function my_error($header)
	{
	  global $conf;
	  
	  $error = '<pre>';
	  $error.= $header;
	  $error.= '[mysql error '.mysql_errno().'] ';
	  $error.= mysql_error();
	  $error.= '</pre>';
	  echo $error;
	}
	
	function query($sql, $unbuffered = false)
	{
		if (defined('BABILI_SHOW_QUERIES'))
			$q_start = get_microtime();

		if ($unbuffered)
			$this->query_result = @mysql_unbuffered_query($sql, $this->link_id);
		else
			$this->query_result = mysql_query($sql, $this->link_id) or $this->my_error($sql."\n");

		if ($this->query_result)
		{
			if (defined('BABILI_SHOW_QUERIES'))
				$this->saved_queries[] = array($sql, sprintf('%.5f', get_microtime() - $q_start));

			++$this->num_queries;

			return $this->query_result;
		}
		else
		{
			if (defined('BABILI_SHOW_QUERIES'))
				$this->saved_queries[] = array($sql, 0);

			return false;
		}
	}


	function result($query_id = 0, $row = 0)
	{
		return ($query_id) ? @mysql_result($query_id, $row) : false;
	}


	function fetch_assoc($query_id = 0)
	{
		return ($query_id) ? @mysql_fetch_assoc($query_id) : false;
	}


	function fetch_row($query_id = 0)
	{
		return ($query_id) ? @mysql_fetch_row($query_id) : false;
	}

    function fetch_array($query_id = 0)
	{
		return ($query_id) ? @mysql_fetch_array($query_id) : false;
	}

	function num_rows($query_id = 0)
	{
		return ($query_id) ? @mysql_num_rows($query_id) : false;
	}


	function affected_rows()
	{
		return ($this->link_id) ? @mysql_affected_rows($this->link_id) : false;
	}


	function insert_id()
	{
		return ($this->link_id) ? @mysql_insert_id($this->link_id) : false;
	}


	function get_num_queries()
	{
		return $this->num_queries;
	}


	function get_saved_queries()
	{
		return $this->saved_queries;
	}


	function free_result($query_id = false)
	{
		return ($query_id) ? @mysql_free_result($query_id) : false;
	}


	function escape($str)
	{
		if (function_exists('mysql_real_escape_string'))
			return mysql_real_escape_string($str, $this->link_id);
		else
			return mysql_escape_string($str);
	}

	function escape_array($array_to_escape) {
		foreach ($array_to_escape as $key => $element) {
		  if(!is_array($element)) {
			$array_to_escape[$key] = $this->escape($element);
		  }
		  else {
			$array_to_escape[$key] = $this->escape_array($element);
		  }
		}
		return $array_to_escape;
	}

	function error()
	{
		$result['error_sql'] = @current(@end($this->saved_queries));
		$result['error_no'] = @mysql_errno($this->link_id);
		$result['error_msg'] = @mysql_error($this->link_id);

		return $result;
	}


	function close()
	{
		if ($this->link_id)
		{
			if ($this->query_result)
				@mysql_free_result($this->query_result);

			return @mysql_close($this->link_id);
		}
		else
			return false;
	}

}
?>